package com.walter.dynamic.thread.pool;

import com.walter.dynamic.thread.pool.core.AdaptiveThreadPoolAdjuster;
import com.walter.dynamic.thread.pool.core.DynamicThreadPoolExecutor;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author Walter
 * @date 2025/1/24 下午9:06
 **/
@Slf4j
public class ThreadPoolDemo {
    public static void main(String[] args) {
        // 创建动态线程池
        DynamicThreadPoolExecutor threadPool = new DynamicThreadPoolExecutor(
                2,// 初始核心线程数
                10, // 最大线程数
                60,
                100);
        // 创建自适应调整器
        AdaptiveThreadPoolAdjuster adjuster = new AdaptiveThreadPoolAdjuster(threadPool,2,8 );
        // 启动自适应调整
        adjuster.startAdjustment(5, TimeUnit.SECONDS);

        // 模拟任务提交
        for (int i = 0; i < 1000; i++) {
            final int taskId = i;
            threadPool.execute(() -> {
                try {
                    // 模拟任务执行时间
                    Thread.sleep(new Random().nextInt(1000));
                    log.info("Task {} completed",taskId);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
            });
        }
        CompletableFuture.runAsync(()->{}, threadPool.getExecutor());
    }
}
